//
//  EditSettingsViewController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/12/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "EditSettingsViewController.h"
#import "GruphotoTabBarController.h"
#import "RegisterViewController.h"
#import "AboutGruphotoViewController.h"
#import <FacebookSDK/FacebookSDK.h>

@interface EditSettingsViewController ()

@end

@implementation EditSettingsViewController
@synthesize editSettingTableView, fieldsLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title_gruphoto.png"]];
        self.navigationItem.titleView = titleImageView;
        [titleImageView release];
        
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setBounds:CGRectMake(0, 0, 51, 30)];
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"button_cancel.png"] forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
        self.navigationItem.leftBarButtonItem = cancelButtonItem;
        [cancelButtonItem release];
        
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [doneButton setBounds:CGRectMake(0, 0, 51, 30)];
        [doneButton setBackgroundImage:[UIImage imageNamed:@"button_done.png"] forState:UIControlStateNormal];
        [doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
        self.navigationItem.rightBarButtonItem = doneButtonItem;
        [doneButtonItem release];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:231/255.0 blue:237/255.0 alpha:1.0];
    editSettingTableView.backgroundColor = [UIColor clearColor];
    editSettingTableView.opaque = NO;
    NSArray *section1 = [NSArray arrayWithObjects:@"The Blue", @"Edit profile", @"About Gruphoto", nil];
    NSArray *section2 = [NSArray arrayWithObjects:@"Facebook", @"Twitter", @"Tumblr", nil];
    self.fieldsLabel = [NSMutableArray arrayWithObjects:section1, section2, nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    GruphotoTabBarController *tabBarController = (GruphotoTabBarController *)self.tabBarController;
    [tabBarController hidesFakeTabBar:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.editSettingTableView = nil;
}

- (void)dealloc {
    self.fieldsLabel = nil;
    [editSettingTableView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Action methods
- (IBAction)backButtonPressed:(id)sender {
    GruphotoTabBarController *tabBarController = (GruphotoTabBarController *)self.tabBarController;
    [UIView animateWithDuration:0.2 animations:^{
        [tabBarController showFakeTabBar:YES];
    } completion:^(BOOL finished) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (IBAction)doneButtonPressed:(id)sender {
    GruphotoTabBarController *tabBarController = (GruphotoTabBarController *)self.tabBarController;
    [UIView animateWithDuration:0.2 animations:^{
        [tabBarController showFakeTabBar:YES];
    } completion:^(BOOL finished) {
        [self.navigationController popViewControllerAnimated:YES];
    }];    
}

- (IBAction)logoutButtonPressed:(id)sender {
    [FBSession.activeSession closeAndClearTokenInformation];    
    [self.tabBarController dismissModalViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"Account";
    }
    return @"Sharing";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *EditCellId = @"EditCellId";
    UITableViewCell *cell = nil;
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EditCellId] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section == 0) {
            switch (indexPath.row) {
                case 0: {
                    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [logoutButton setBounds:CGRectMake(0, 0, 60, 30)];
                    [logoutButton setImage:[UIImage imageNamed:@"button_logout_red.png"] forState:UIControlStateNormal];
                    [logoutButton addTarget:self action:@selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
                    cell.accessoryView = logoutButton;
                    break;
                }
                case 1:
                case 2:
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                default:
                    break;
            }
        }
        else {
            UIButton *accessButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [accessButton setBounds:CGRectMake(0, 0, 60, 30)];
            if (indexPath.row == 0) {
                [accessButton setImage:[UIImage imageNamed:@"button_logout_green.png"] forState:UIControlStateNormal];
            }
            else {
                [accessButton setImage:[UIImage imageNamed:@"button_login_default.png"] forState:UIControlStateNormal];                
            }
            cell.accessoryView = accessButton;
        }
    }
    cell.textLabel.text = [[fieldsLabel objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            RegisterViewController *registerViewController = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController"
                                                                                                      bundle:nil];
            registerViewController.editMode = AccountEditModeUpdate;
            [self.navigationController pushViewController:registerViewController animated:YES];
            [registerViewController release];            
        }
        else if (indexPath.row == 2) {
            AboutGruphotoViewController *aboutViewController = [[AboutGruphotoViewController alloc] initWithNibName:@"AboutGruphotoViewController" bundle:nil];
            [self.navigationController pushViewController:aboutViewController animated:YES];
            [aboutViewController release];
        }
    }
}

@end
